package com.ruoyi.article.service.impl;

import com.ruoyi.article.domain.Classify;
import com.ruoyi.article.mapper.ClassifyMapper;
import com.ruoyi.article.service.ClassifyService;
import com.ruoyi.common.core.constant.ClassifyConstants;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClassifyServiceImpl implements ClassifyService {



    @Autowired
    private ClassifyMapper classifyMapper;

    /**
     * 检查分类的唯一性
     * @param classify
     * @return
     */
    @Override
    public boolean checkClassifyUnique(Classify classify) {
        Long classifyId = StringUtils.isNull(classify.getClassifyId()) ? -1 : classify.getClassifyId();
        Classify classifyCheck = classifyMapper.checkClassifyUnique(classify.getName());
        if (StringUtils.isNotNull(classifyCheck) && classifyCheck.getClassifyId().longValue() != classifyId.longValue()) {
            return ClassifyConstants.NOT_UNIQUE;
        }
        return ClassifyConstants.UNIQUE;
    }

    /**
     * 插入分类
     * @param classify
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertClassify(Classify classify) {
        return classifyMapper.insertClassify(classify);
    }

    /**
     * 查询分类列表
     * @param classify
     * @return
     */
    @Override
    public List<Classify> selectClassifyList(Classify classify) {
        return classifyMapper.selectClassifyList(classify);
    }

    /**
     * 依据id查询分类
     * @param classifyId
     * @return
     */
    @Override
    public Classify selectClassifyById(Long classifyId) {
        return classifyMapper.selectClassifyById(classifyId);
    }

    /**
     * 依据ids删除分类
     * @param classifyIds
     * @return
     */
    @Override
    public int deleteClassifyByIds(Long[] classifyIds) {
        return classifyMapper.deleteClassifyByIds(classifyIds);
    }

    /**
     * 依据id删除分类
     * @param classifyId
     * @return
     */
    @Override
    public int deleteClassifyById(Long classifyId) {
        return classifyMapper.deleteClassifyById(classifyId);
    }

    /**
     * 更新分类
     * @param classify
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateClassify(Classify classify) {
        Classify newParentClassify = classifyMapper.selectClassifyById(classify.getParentId());
        Classify oldClassify = classifyMapper.selectClassifyById(classify.getClassifyId());
        if (StringUtils.isNotNull(newParentClassify) && StringUtils.isNotNull(oldClassify))
        {
            String newAncestors = newParentClassify.getAncestors() + "," + newParentClassify.getClassifyId();
            String oldAncestors = oldClassify.getAncestors();
            classify.setAncestors(newAncestors);
            updateClassifyChildren(classify.getClassifyId(), newAncestors, oldAncestors);
        }
        int result = classifyMapper.updateClassify(classify);
        if (UserConstants.DEPT_NORMAL.equals(classify.getStatus()) && StringUtils.isNotEmpty(classify.getAncestors())
                && !StringUtils.equals("0", classify.getAncestors()))
        {
            // 如果该分类是启用状态，则启用该分类的所有上级部门
            updateParentClassifyStatusNormal(classify);
        }
        return result;
    }

    /**
     * 修改该分类的父级状态
     *
     * @param classify 当前部门
     */
    private void updateParentClassifyStatusNormal(Classify classify)
    {
        String ancestors = classify.getAncestors();
        Long[] classifyIds = Convert.toLongArray(ancestors);
        classifyMapper.updateClassifyStatusNormal(classifyIds);
    }


   /**  修改子元素关系*/
    public void updateClassifyChildren(Long classifyId, String newAncestors, String oldAncestors)
    {
        List<Classify> children = classifyMapper.selectChildrenClassifyById(classifyId);
        for (Classify child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            classifyMapper.updateClassifyChildren(children);
        }
    }

    /**
     * 根据ID查询所有子分类
     * @param classifyId
     * @return
     */
    @Override
    public int selectNormalChildrenClassifyById(Long classifyId)
    {
        return classifyMapper.selectNormalChildrenClassifyById(classifyId);
    }


    /**
     * 是否存在子分类
     * @param classifyId
     * @return
     */
    @Override
    public boolean hasChildByClassifyId(Long classifyId)
    {
        int result = classifyMapper.hasChildByClassifyId(classifyId);
        return result > 0;
    }

    /**
     * 校验分类是否存在记录
     *
     * @return 结果
     */
    @Override
    public boolean checkClassifyExistRecord(Long classifyId)
    {
        Classify temp = classifyMapper.selectClassifyById(classifyId);
        int result = classifyMapper.checkClassifyExistRecord(temp.getName());

        return result > 0;
    }
}
